<template>
	<view class="configuration">
		<view class="top-box">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>标签管理</el-breadcrumb-item>
				<el-breadcrumb-item>{{ getBreadcrumb }}</el-breadcrumb-item>
			</el-breadcrumb>
			
			<view class="header-serch flex ac jcsb">
				<view class="search-box flex ac">
					<view class="search flex ac">
						<view class="search-title" v-if="!second">名称：</view>
						<!-- <view class="search-title" v-else>渠道：</view> -->
						<el-input v-model="name" placeholder="请输入名称"></el-input>
					</view>
					<view class="search-btn">
						<el-button type="primary" class="search-button" @click='search'>查询</el-button>
					</view>
				</view>
				<el-button class="add-user" type="primary" @click='openDelog(0)'>+ 新增二维码</el-button>
			</view>
			
			<el-table 
				:data="Table.TableDataList" 
				style="width: 100%;" 
				height="calc(100vh - 298px)"
				stripe
				:header-cell-style="{background:'#E9ECEE',color:'#16171F'}">
				<el-table-column 
					show-overflow-tooltip 
					:prop="item.prop" 
					:label="item.label" 
					v-for="(item,index) in newTitleList"
					:key="index"
					:min-width="item.width">
					<template slot-scope="scope">
						<!-- 每一项的值 -->
						<!-- <span v-if="item.prop && item.prop != 'activityUrl' && item.prop != 'qrStatus'">{{ scope.row[item.prop] }}</span> -->
						<span v-if="item.prop && item.prop != 'qrCodeLink' && item.prop != 'qrStatus'">{{ (item.prop == 'createTime' || item.prop == 'updateTime')? new Date(scope.row[item.prop]).format():scope.row[item.prop] }}</span>
						<span v-if="item.prop && item.prop == 'qrStatus'">{{ scope.row[item.prop] ? '停用':'正常' }}</span>
						
						<view v-if="item.prop && item.prop == 'qrCodeLink'" class="qr-code-box">
						<!-- <view v-if="item.prop && item.prop == 'activityUrl'" class="qr-code-box"> -->
							<!-- <tki-qrcode
								:cid="scope.row['id'] + ''"
							    :val="scope.row[item.prop]"
							    :size="50"
								:loadMake="true"
								:onval="true"
								unit="'px'"
							    @result="qrR" /> -->
							<image :src="scope.row['qrCodeLink']" style="width: 50px;height: 50px;display: block;" mode="widthFix"></image>
						</view>
						
						<!-- 操作项 -->
						<!-- 二维码 -->
						<view v-if="!item.prop && !second">
							<el-button class="start-color" @click="openDelog(1, scope.row)" type="text" size="small">预览下载</el-button>
							<el-button class="start-color" @click="copyClick(scope.row)" type="text" size="small">复制活动链接</el-button>
						</view>
					</template>
				</el-table-column>
			</el-table>
		</view>
		<view class="footer">
			<el-pagination  
				@size-change="handleSizeChange" 
				@current-change="handleCurrentChange"
				:current-page="Table.page"  
				layout="total, prev, pager, next, jumper, sizes" 
				background 
				:total="Table.count"
				:page-sizes="[10, 12]" 
				:page-size.sync="Table.limit">
			</el-pagination>
			<view class="pageLimit">当前显示{{Table.limit}}条/页</view>
		</view>
		
		<!-- <confirm @confirmFlagClick="confirmFlagClick" :confirmFlag="confirmFlag" :title="confirmTitle"></confirm> -->
		<labelDialog :dialogVisible="addUserDialogFlag" @closeDialog="closeDialog" :title="addDialogTitle" :code="currentCode" :currentCodeName="currentCodeName"></labelDialog>
	</view>
</template>

<script>
	import { Request } from '../model/request.js'
	import { Util } from '../utils/util.js'
	
	// import confirm from './dialog/confirm.vue'
	import labelDialog from './dialog/labelDialog.vue'
	import tkiQrcode from "./tki-qrcode/tki-qrcode.vue"
	 
	export default {
		components: { labelDialog, tkiQrcode },
		
		props: {
			second: {
				type: [String, Number],
				default: 0
			}
		},
		
		computed: {
			getBreadcrumb() {
				let c = '二维码'
				// if (this.second == 1) {
				// 	c = '活动配置'
				// } else if (this.second == 2) {
				// 	c = '广告链接'
				// }
				return c
			},
			
			newTitleList() {
				// if (this.second == 1) {
				// 	return this.Table.titleList1
				// } else if (this.second == 2) {
				// 	return this.Table.titleList2
				// } else {
				// 	return this.Table.titleList
				// }
				return this.Table.titleList
			}
		},
		
		data() {
			return {
				phone: '',
				name: '',
				Table: {
					titleList: [{
							prop: "qrName",
							label: "名称"
						},
						{
							prop: "qrCodeLink",
							label: "二维码"
						},
						{
							prop: "activityUrl",
							label: "链接",
							width: '130px'
						},
						{
							prop: "serialNumber",
							label: "标签编号"
						},
						{
							prop: "qrStatus",
							label: "状态",
							width: '100px'
						},
						{
							prop: "createTime",
							label: "创建时间"
						},
						{
							prop: "updateTime",
							label: "更新时间"
						},
						{
							label: "操作",
							width: '130px'
						}
					],
					TableDataList: [],
					result: '',
					page: 1,
					limit: 10,
					count: 0
				},
				
				addUserDialogFlag: false,
				addDialogTitle: '',
				currentCode: '',
				currentCodeName: ''
			}
		},
		
		mounted() {
			this.initTable()
		},
		
		watch: {
			second(newVal, oldVal) {
				this.phone = ''
				this.name = ''
				this.Table.page = 1
				this.Table.limit = 10
				this.Table.count = 0
				this.initTable()
			}
		},
		
		methods: {
			search() {
				console.log(this.name)
				this.initTable(1, this.name)
			},
			
			async initTable(page, name='') {
				this.Table.TableDataList = []
				if (!Util.isNull(page)) {
					this.Table.page = page
				} else {
					this.Table.page = 1
				}
				let res = await Request.QRCODEFINDALL(this.Table.page, this.Table.limit, name)
				if (!Util.isNull(res)) {
					this.Table.TableDataList = res.data.records
					this.Table.count = res.data.total
				} else {
					this.Table.TableDataList = null
				}
			},
			
			// 用户，活动，广告 【创建用户，新增链接】
			openDelog(i, item=null) {
				if (i) {
					this.addDialogTitle = '下载预览二维码'
					// this.currentCode = item.activityUrl
					this.currentCode = item.qrCodeLink
					this.currentCodeName = item.qrName
				} else {
					this.addDialogTitle = '新增二维码'
				}
				this.addUserDialogFlag = true
			},
			
			closeDialog(flag) {
				this.addUserDialogFlag = false
				if (typeof flag != 'function' && flag) { // 新增二维码成功
					this.initTable(this.Table.page, this.name)
				}
			},
			
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.initTable(1, this.name)
				// this.name = ''
			},
			
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`, this.Table.page);
				this.initTable(val, this.name)
			},
			
			copyClick(item) { // 复制活动链接
				Util.copy(item.activityUrl)
			},
			
			qrR(e) {
				console.log(e, 'qrR')
			}
		}
	}
</script>

<style scoped>
	.configuration {
		/* padding: 20px 40px 50px; */
		position: relative;
	}
	
	.top-box {
		padding: 20px 40px 0;
	}
	
	.header-serch {
		margin: 30px 0;
	}
	
	.search-title {
		min-width: 44px;
		font-size: 14px;
		font-weight: 500;
		color: #16171F;
		/* text-align: right; */
	}
	
	.search-btn {
		margin-left: 30px;
	}
	
	.footer {
		width: 100%;
		height: 40px;
		display: flex;
		align-items: center;
		/* position: absolute; */
		/* left: 0; */
		/* bottom: 0px; */
		justify-content: center;
		height: 130px;
		background: #FFFFFF;
		box-shadow: 0px -7px 9px 0px rgba(0, 0, 0, 0.02);
	}
	
	.del-color {
		color: #D1141F;
	}
	
	.start-color {
		color: #4A92FD;
	}
	
	>>> .el-input__inner{
		height: 32px;
	}
	
	.search-button, .add-user {
		font-size: 14px;
		height: 32px;
		line-height: 32px;
		padding-top: 0;
		padding-bottom: 0;
	}
	
	.qr-code-box {
		height: 50px;
	}
	
	.tki-qrcode {
		height: 100%;
	}
</style>
